package com.seventeen.monster.web.controller.proxy;

import com.seventeen.monster.common.annotation.Log;
import com.seventeen.monster.common.constant.DelFlagConstants;
import com.seventeen.monster.common.core.controller.BaseController;
import com.seventeen.monster.common.core.domain.AjaxResult;
import com.seventeen.monster.common.core.page.TableDataInfo;
import com.seventeen.monster.common.enums.BusinessType;
import com.seventeen.monster.mq.proxy.domain.MqConsumerProxy;
import com.seventeen.monster.mq.proxy.service.IMqConsumerProxyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 消费代理
 *
 * @author seventeen
 */
@RestController
@RequestMapping(value = "/mq/consumer/proxy")
public class MqConsumerProxyController extends BaseController {

    @Autowired
    private IMqConsumerProxyService mqConsumerProxyService;

    @PreAuthorize("@ss.hasPermi('mq:consumer:list')")
    @GetMapping("/list")
    public TableDataInfo list(MqConsumerProxy mqConsumerProxy)
    {
        startPage();
        List<MqConsumerProxy> list = mqConsumerProxyService.queryList(mqConsumerProxy);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('mq:consumer:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getConsumer(@PathVariable Long id)
    {
        MqConsumerProxy mqConsumerProxy = new MqConsumerProxy();
        mqConsumerProxy.setId(id);
        List<MqConsumerProxy> list = mqConsumerProxyService.queryList(mqConsumerProxy);
        return success(CollectionUtils.isEmpty(list)?null:list.get(0));
    }

    @PreAuthorize("@ss.hasPermi('mq:consumer:add')")
    @Log(title = "消费者管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MqConsumerProxy mqConsumerProxy)
    {
        mqConsumerProxy.setCreateBy(getUsername());
        mqConsumerProxyService.add(mqConsumerProxy);
        return success();
    }

    @PreAuthorize("@ss.hasPermi('mq:consumer:edit')")
    @Log(title = "消费者管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody MqConsumerProxy mqConsumerProxy)
    {
        if (mqConsumerProxy.getId() == null)
        {
            return error("请求参数有误");
        }
        mqConsumerProxy.setUpdateBy(getUsername());
        mqConsumerProxyService.update(mqConsumerProxy);
        return success();
    }

    /**
     * 删除消费者
     */
    @PreAuthorize("@ss.hasPermi('mq:consumer:remove')")
    @Log(title = "消费者管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        MqConsumerProxy mqConsumerProxy = new MqConsumerProxy();
        mqConsumerProxy.setId(id);
        mqConsumerProxy.setDelFlag(DelFlagConstants.DELETED);
        mqConsumerProxy.setUpdateBy(getUsername());
        mqConsumerProxyService.update(mqConsumerProxy);
        return success();
    }

}
